package com.work.example.demo5.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.example.demo5.Mapper.AuthorMapper;
import com.work.example.demo5.Mapper.BookMapper;
import com.work.example.demo5.Mapper.BorrowMapper;
import com.work.example.demo5.domain.BorrowEntity;
import com.work.example.demo5.domain.vo.BorrowEntityVo;
import com.work.example.demo5.domain.vo.BorrowformVo;
import com.work.example.demo5.repo.UserEntityRepository;
import com.work.example.demo5.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

@Service
public class BorrowServiceImpl extends ServiceImpl<BorrowMapper, BorrowEntity> implements BorrowService {
    @Autowired
    private UserEntityRepository userEntityRepository;
    @Autowired
    private BookMapper bookMapper;
    @Override
    public List<BorrowEntityVo> getByName(String username) {
        List<BorrowEntityVo> byName = baseMapper.getByName(username);
        for (BorrowEntityVo borrowEntityVo : byName) {
            String byBorrow = getByBorrow(borrowEntityVo.getBorrowId());
            borrowEntityVo.setBorrowByName(byBorrow);
        }
        return byName;
    }

    @Override
    public String getByBorrow(Long borrowId) {
        return baseMapper.getByBorrow(borrowId);
    }

    @Override
    public List<BorrowEntityVo> getallborrow() {
        List<BorrowEntityVo> allborrow = baseMapper.getallborrow();
        for (BorrowEntityVo borrowEntityVo : allborrow) {
            String byBorrow = getByBorrow(borrowEntityVo.getBorrowId());
            borrowEntityVo.setBorrowByName(byBorrow);
        }
        return allborrow;
    }

    @Override
    public int deletebyId(Long id) {
        return baseMapper.deletebyId(id);
    }

    @Override
    public int saveBorrow(BorrowformVo borrowformVo) {
        Long borrowByName = baseMapper.getBorrowByName(borrowformVo.getBorrowBy());
        bookMapper.changebookstatus(borrowformVo.getBookId());
        BorrowEntity borrowEntity =
                new BorrowEntity(null,borrowformVo.getBookId(),
                        borrowformVo.getUserId(), LocalTime.now().toString(),null,null,borrowByName);
        return baseMapper.saveBorrow(borrowEntity);

    }

    @Override
    public int updateborrow(BorrowEntityVo borrowEntityVo) {
        Long userId = userEntityRepository.getUserId(borrowEntityVo.getUserName());
        Long idByName = bookMapper.getIdByName(borrowEntityVo.getBookName());
        String i = new String("0");
        if (borrowEntityVo.getIsReturned().equals("已还")){
            i="1";
        }

        BorrowEntity borrowEntity =
                new BorrowEntity(
                        borrowEntityVo.getBorrowId(),
                        idByName,userId,
                        borrowEntityVo.getBorrowTime(),
                        borrowEntityVo.getReturnTime(),i,null);
        return baseMapper.updateborrow(borrowEntity);
    }

}
